﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace OneDayModel.Web
{
    public partial class UploadPicture : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void uploadPictureBtn_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                if (FileUpload1.PostedFile.ContentType == "image/pjpeg" ||
                    FileUpload1.PostedFile.ContentType == "image/jpeg")
                {
                    Label1.Text = FileUpload1.PostedFile.ContentType;
                    Label2.Text = FileUpload1.PostedFile.ContentLength.ToString();
                    Label3.Text = Path.GetFileName(FileUpload1.PostedFile.FileName);

                    string name = Path.GetRandomFileName() + ".jpg";
                    string fileName = MapPath(@"\Albums\") + name;
                    
                    FileUpload1.SaveAs(fileName);

                    Image image = Image.FromStream(FileUpload1.PostedFile.InputStream);

                    image.Save(fileName, ImageFormat.Jpeg);

                    double r = 128.0 / Math.Max(image.Width, image.Height);

                    Image thumbnailImage = image.GetThumbnailImage((int) (image.Width * r), (int) (image.Height * r), ThumbnailCallback, IntPtr.Zero);

                    thumbnailImage.Save(MapPath(@"\Albums\Thumbs\") + name, ImageFormat.Jpeg);

//<!--
//       <asp:HyperLink ID="HyperLink1" runat="server">
//        <asp:Image ID="Image1" runat="server" Height="124px" Width="124px" />
//        </asp:HyperLink>
//->        

                    Image1.Src = @"\Albums\Thumbs\" + name;
                    Image1.Width = (int) (image.Width*r);
                    Image1.Height = (int) (image.Height*r);

                    HyperLink1.HRef = @"\Albums\" + name;
                }
            }
        }

        private bool ThumbnailCallback()
        {
            return true;
        }
    }
}
